import {getDictDropReq, getOneUserInfoReq} from '@/api/admin/user.ts'
import {getAreaListDropBoxReq, getValueByKeyReq} from "@/api/common/common.ts";
import {
    NOTIFICATION_DURING,
    NOTIFICATION_POSITION,
    NOTIFICATION_SEND_WAY_WEB
} from "@/constants/common-constant.ts";
import {getNotReadInfoReq, updateNotificationReadReq} from "@/api/admin/notification.ts";
import {RESPONSE_CODE_SUCCESS} from "@/config/common-config.ts";
import {ElNotification} from "element-plus";

/**
 * 统一获取字典数据接口
 * @param dictKey 字典key
 */
export const dictDataUtil = async (dictKey: string):Promise<any> => {
    const params = {
        dictionaryKey: dictKey
    }
    const res: any = await getDictDropReq(params);

    return res.data
}

/**
 * 统一获取地域下拉框数据
 * @param parentCode 父级code
 * @param keyword 查询值
 */
export const areaDataUtil = async (parentCode: string, keyword: string):Promise<any> => {
    const params = {
        parentCode: parentCode,
        keyword: keyword
    }
    const res: any = await getAreaListDropBoxReq(params);

    return res.data
}

/**
 * 获取参数信息
 * @param tagKey
 * @param paramKey
 */
export const getParamValueUtil = async (tagKey: string, paramKey: string):Promise<any> => {
    const params = {
        tagKey: tagKey,
        paramKey: paramKey
    }
    // const res: any = await getValueByKeyReq(params);
    // return res.data

    return 4;
}

/**
 * 获取单体用户信息
 * @param userId 用户id
 */
export const getOneUserInfoUtil = async (userId: string):Promise<any> => {
    const params = {
        id: userId
    }
    const res: any = await getOneUserInfoReq(params);
    return res.data
}

/**
 * 获取未读消息
 */
export const getNotReadMessageUtil = async () => {
    const params = {
        sendWay: NOTIFICATION_SEND_WAY_WEB
    }

    const res: any = await getNotReadInfoReq(params);
    if (res.code === RESPONSE_CODE_SUCCESS){
        for (const notification of res.data){
            ElNotification({
                title: notification.notificationTitle,
                message: notification.notificationContent,
                duration: notification.duration || NOTIFICATION_DURING,
                position: notification.position || NOTIFICATION_POSITION,
                onClick: readNotificationFn(notification.id),
                onClose(vm) {
                    readNotificationFn(notification.id)
                },
            })
        }
    }
}

const readNotificationFn = (id: string) => {
    const params = {
        id: id
    }
    updateNotificationReadReq(params).then()

    return () => {}
}

